import time
from bs4 import BeautifulSoup
import httpx
import requests

from entitys.AiBaseEntity import AiBase, add


def get_html():
    response = requests.get("https://app.chinaz.com/djflkdsoisknfoklsyhownfrlewfknoiaewf/ai/GetTopAiProductVisitList.aspx?_t=1704342293&page=1&pagesize=100&f=&o=&ticket=0f667e4a-9a0b-9242-e577-2509608a2c17")
    if response.status_code == 200:
        data = response.json()
        res = data["data"]["results"]
        for item in res:
            Id = item["Id"]
            Title = item["Title"]
            Summary = item["Summary"]
            Url = item["Url"]
            time.sleep(1)
            get_detail(Url, Title, Summary)
    else:
        print("请求失败")



def getUrl(url):
    # 发送请求获取网页内容
    client = httpx.Client(http2=True)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
        'Cookie': 'BIDUPSID=457C3455302D430C9674CAC89AF78CFA; PSTM=1657781295; BAIDUID=CE87CE2967A79BC2C0617B8BA1AC45B3:SL=0:NR=10:FG=1; BD_UPN=12314753; MCITY=-176%3A; H_WISE_SIDS_BFESS=234020_216839_213354_214792_110085_244727_259031_261710_236312_256419_265881_266354_265615_267074_268405_268593_266186_259642_269232_269783_268236_269905_267066_256739_270460_270519_270603_270547_271019_271173_256958_267659_271319_270102_271879_269857_271813_271935_234296_234208_269296_272082_272226_270055_272279_267596_272336_253022_272653_272836_260335_272986_273061_267558_273160_273164_273138_273215_273263_273238_273301_273400_273392_271158_273455_273473_273519_271562_271146_273703_273318_264170_272262_263619_274082_273931_273965_274142_269610_273918_273044_273598_272695_272859_256223_272562_274426_274441_188333_274649_274765_274759_274822_274839_274844_274856_274847_270158_274870_273981_275070_272801_274452_267807_267547_275145_272333_275167_275197_275147_272318_8000051_8000108_8000124_8000142_8000160_8000162_8000165_8000170_8000183_8000186_8000204; sug=3; sugstore=0; ORIGIN=0; bdime=0; ispeed_lsm=2; H_WISE_SIDS=39635_39668_39672_39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Q0KKJyLR-8Kb7VbPjoMUnkbfJBDGOUQbjkK56e2JRkQIbvennJhf6AXx47yajK2boGQG58-UbuQtoFDqrP0UjpQT8ryJ_OK5OiJgDqslnNab3vOPTNXpO15jkzBN5thURB2DkO-4bCWJ5TMl5jDh3Mb6ksDMDtqj0OtbkO_Ktyf-3bfTrx5tQHMDCShGRHafo9WDTm_D_5H4JdjKQl0nJcKh33yP6zK-o30K6Z-pPKKR70sPjoht6obj8VMnrrBJcB3mkjbpQGfn02OP5EQUrcW44syPR8KxRnWTIOKfA-b4ncjRcTehoM3xI8LNj405OTbIFO0KJyJCFhhKPGD6uMD5PfbeTXatb8-bCX3buQQh4V8pcNLTDK5fAq5UAtblJM-n6n_pvFytQaDt5kjlO1j4_eKpOg0fbhyTQJ-l4aytOzSh5jDh3PXjksD-RlanJtKeoy0hvcMR5cShnGeMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t225QhDG-DJTkjJR-s_C8atTrWeIbcbtI_-P4DePO73xRZ5mAqoqOHKfT4V4jKyU5J350UyR6MBMne-C7naIQqah0WVhOTbMjsqj53XqbNWJO43bRTW-Py5KJvfJopKUTChP-Uypr-Wh37bRblMKoaMp78jR093JO4y4Ldj4oxJpOJ5JbMonLafDKahK-ljjK-eP0OqxJO243fHD7XVK5aWl7keq8CD4c2b4PVylQpXtbXQm6w_R3RBJ3IDPQ2y5jHhnIXytRaal58BbnRoqRK2lRpsIJMQxFWbT8UQx79KPkeaKvia-THBMb1fDnDBT5h2M4qMxtOLR3pWDTm_q5TtUJMeCnTDbAhe4tX-NFJJ5KtJx5; H_PS_645EC=2e7cTUbugOqhTVjLNzr5%2FDiuawzqN3FtHO%2Bk%2BIlcJWq46kZzYLKgFusysCA; COOKIE_SESSION=171_1_7_7_8_24_1_0_6_7_1_7_591_0_0_0_1700736994_1700706226_1700737162%7C9%23175549_11_1700706222%7C9; WWW_ST=1700737266076'
    }

    response = client.get("https://top.aibase.com" + url, headers=headers)
    html_content = response.content
    # 使用BeautifulSoup解析网页内容
    soup = BeautifulSoup(html_content, 'html.parser')
    scripts = soup.find_all('script')
    for script in scripts:
        if 'window.__NUXT__=' in script.text:
            url = script.text.split('window.__NUXT__=')[1].split(';')[2]
            decoded_url = url.split('=')[1].replace('\\u002F', '/').replace('\"', '')
            break
def get_detail(url, Title, Summary):
    AiBase.Name = Title
    AiBase.Summary = Summary
    # 发送请求获取网页内容
    client = httpx.Client(http2=True)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
        'Cookie': 'BIDUPSID=457C3455302D430C9674CAC89AF78CFA; PSTM=1657781295; BAIDUID=CE87CE2967A79BC2C0617B8BA1AC45B3:SL=0:NR=10:FG=1; BD_UPN=12314753; MCITY=-176%3A; H_WISE_SIDS_BFESS=234020_216839_213354_214792_110085_244727_259031_261710_236312_256419_265881_266354_265615_267074_268405_268593_266186_259642_269232_269783_268236_269905_267066_256739_270460_270519_270603_270547_271019_271173_256958_267659_271319_270102_271879_269857_271813_271935_234296_234208_269296_272082_272226_270055_272279_267596_272336_253022_272653_272836_260335_272986_273061_267558_273160_273164_273138_273215_273263_273238_273301_273400_273392_271158_273455_273473_273519_271562_271146_273703_273318_264170_272262_263619_274082_273931_273965_274142_269610_273918_273044_273598_272695_272859_256223_272562_274426_274441_188333_274649_274765_274759_274822_274839_274844_274856_274847_270158_274870_273981_275070_272801_274452_267807_267547_275145_272333_275167_275197_275147_272318_8000051_8000108_8000124_8000142_8000160_8000162_8000165_8000170_8000183_8000186_8000204; sug=3; sugstore=0; ORIGIN=0; bdime=0; ispeed_lsm=2; H_WISE_SIDS=39635_39668_39672_39664_39676_39679_39712_39751_39766_39791_39704_39688; BDUSS=lJMeHZRYmRpSXE0UGp6dk5USW5RaXd2VXRYcFNrVFFzUk5SeEh1aEtYaVd0WUpsRVFBQUFBJCQAAAAAAAA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kzBN5thURB2DkO-4bCWJ5TMl5jDh3Mb6ksDMDtqj0OtbkO_Ktyf-3bfTrx5tQHMDCShGRHafo9WDTm_D_5H4JdjKQl0nJcKh33yP6zK-o30K6Z-pPKKR70sPjoht6obj8VMnrrBJcB3mkjbpQGfn02OP5EQUrcW44syPR8KxRnWTIOKfA-b4ncjRcTehoM3xI8LNj405OTbIFO0KJyJCFhhKPGD6uMD5PfbeTXatb8-bCX3buQQh4V8pcNLTDK5fAq5UAtblJM-n6n_pvFytQaDt5kjlO1j4_eKpOg0fbhyTQJ-l4aytOzSh5jDh3PXjksD-RlanJtKeoy0hvcMR5cShnGeMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t225QhDG-DJTkjJR-s_C8atTrWeIbcbtI_-P4DePO73xRZ5mAqoqOHKfT4V4jKyU5J350UyR6MBMne-C7naIQqah0WVhOTbMjsqj53XqbNWJO43bRTW-Py5KJvfJopKUTChP-Uypr-Wh37bRblMKoaMp78jR093JO4y4Ldj4oxJpOJ5JbMonLafDKahK-ljjK-eP0OqxJO243fHD7XVK5aWl7keq8CD4c2b4PVylQpXtbXQm6w_R3RBJ3IDPQ2y5jHhnIXytRaal58BbnRoqRK2lRpsIJMQxFWbT8UQx79KPkeaKvia-THBMb1fDnDBT5h2M4qMxtOLR3pWDTm_q5TtUJMeCnTDbAhe4tX-NFJJ5KtJx5; H_PS_645EC=2e7cTUbugOqhTVjLNzr5%2FDiuawzqN3FtHO%2Bk%2BIlcJWq46kZzYLKgFusysCA; COOKIE_SESSION=171_1_7_7_8_24_1_0_6_7_1_7_591_0_0_0_1700736994_1700706226_1700737162%7C9%23175549_11_1700706222%7C9; WWW_ST=1700737266076'
    }

    response = client.get("https://top.aibase.com" + url, headers=headers)
    html_content = response.content
    # 使用BeautifulSoup解析网页内容
    soupS = BeautifulSoup(html_content, 'html.parser')
    scripts = soupS.find_all('script')
    for script in scripts:
        if 'window.__NUXT__=' in script.text:
            url = script.text.split('window.__NUXT__=')[1].split(';')[2]
            decoded_url = url.split('=')[1].replace('\\u002F', '/').replace('\"', '')
            AiBase.Url = decoded_url
            break

    # 使用BeautifulSoup解析网页内容
    soup = BeautifulSoup(html_content, 'html.parser')
    # 获取新闻列表
    sdata = soup.find('div', class_='js-detail-real-box')
    label = sdata.find_all('a', class_='com-label')
    AiBase.Label = ""
    for lab in label:
        AiBase.Label += lab.text+","
    name = sdata.find('h2', class_='name').text
    detail = sdata.find('h3', class_='t2').text
    AiBase.Name = name
    AiBase.Detail = detail
    changjing = sdata.find_all('div', class_='detail-dl-div-item')
    for item in changjing:
        itemT = item.find("p", "detail-dl-div-item-t").text
        itemList = item.find_all("p", "detail-dl-div-item-c")
        if itemList != [] and itemT == "需求人群：":
            AiBase.Audience = ""
            for itemC in itemList:
                AiBase.Audience += itemC.text+","
        if itemList != [] and itemT == "使用场景示例：":
            AiBase.ScenarioDemo = ""
            for itemC in itemList:
                AiBase.ScenarioDemo += itemC.text+","
        if itemList != [] and itemT == "产品特色：":
            AiBase.Features = ""
            for itemC in itemList:
                AiBase.Features += itemC.text+","

    print(AiBase.Audience, AiBase.ScenarioDemo, AiBase.Features)
    AiBase.CreateTime = time.time()
    add(AiBase)


if __name__ == '__main__':
    get_html()
    # get_detail("/tool/liblibai")
    # get_detail1("/tool/liblibai")
